package test;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Inspection {

	public static int balanceOld;
	public static float ratingOld;
	public static int kolvoReviewOld;

	public static int balanceCalculated;
	public static float ratingCalculated;

	public static int balanceNew;
	public static float ratingNew;
	public static int kolvoReviewNew;

	public static String dataLastReviewOld = "";

	// переменные для даты/времени последнего осмотра
	public static int year;
	public static int month;
	public static int day;
	public static int hour;
	public static int minutes;
	public static int dayZ;

	// переменные для секущих даты и времени
	public static int curYear;
	public static int curMonth;
	public static int curDay;
	public static int curHour;
	public static int curMinutes;

	static List<Integer> ratingA = new ArrayList<Integer>();

	public static void main(String[] args) throws InterruptedException {

		System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");

		// создаём опцию для хромдрайвера, где указываем размеры страницы
		ChromeOptions option = new ChromeOptions();
		option.addArguments("--window-size=1300,1300");
		WebDriver driver = new ChromeDriver(option);

		// открываем страницу логина, проходим http - авторизацию
		driver.get("http://qa:bfh365Dkjfh@qa.taxi.dancosoft.com/site/login");

		Thread.sleep(1500);

		// вводим логин
		WebElement loginElement = driver.findElement(By.cssSelector(".js-login"));
		loginElement.sendKeys("autotest");

		// вводим пароль
		WebElement element = driver.findElement(By.cssSelector("[type='password']"));
		element.sendKeys("autotest");
		// нажимаем кнопку входа
		WebElement submitElement = driver.findElement(By.cssSelector("button"));
		submitElement.click();

		Thread.sleep(3000);
		addInspection(driver, "emul1");
		// addInspection(driver, "emul2");
		// addInspection(driver, "emul3");
		// addInspection(driver, "emul4");
		// addInspection(driver, "emul5");
		// addInspection(driver, "emul6");
		// addInspection(driver, "emul7");
		// addInspection(driver, "emul8");
		// addInspection(driver, "emul9");
		// addInspection(driver, "emul10");
		// addInspection(driver, "emul11");
		// addInspection(driver, "emul12");
		// addInspection(driver, "emul13");
		// addInspection(driver, "emul14");
		// addInspection(driver, "emul15");
		// addInspection(driver, "emul16");
		// addInspection(driver, "emul17");
		// addInspection(driver, "emul18");
		// addInspection(driver, "emul19");
		// addInspection(driver, "emul20");

		// addInspection(driver, "v41");
		// addInspection(driver, "v42");
		// addInspection(driver, "v43");
		// addInspection(driver, "v44");
		// addInspection(driver, "v45");
		// addInspection(driver, "v46");
		// addInspection(driver, "v47");
		// addInspection(driver, "v48");
		// addInspection(driver, "v49");
		// addInspection(driver, "v50");
		// addInspection(driver, "v51");
		// addInspection(driver, "v52");
		// addInspection(driver, "v53");
		// addInspection(driver, "v54");
		// addInspection(driver, "v55");
		// addInspection(driver, "v56");
		// addInspection(driver, "v57");
		// addInspection(driver, "v58");
		// addInspection(driver, "v59");
		// addInspection(driver, "v60");
		// addInspection(driver, "v61");

	}

//	
	public static int getNumber(WebDriver driver, int kolOptions, ArrayList<Integer> opt) {

		for (int i = 0; i <= 100; i++) {
			try {
				driver.findElement(By.cssSelector("[data-rv-value='newReview.parameters." + i + ".rating']"));
				kolOptions++;
				opt.add(i);
			} catch (Exception e) {
			}
		}
		return kolOptions;
	}

	public static void addInspection(WebDriver driver, String Owner) throws InterruptedException {

		driver.get("http://qa.taxi.dancosoft.com/site/carReview");
		Thread.sleep(2000);
		WebElement searchElement = driver.findElement(By.cssSelector("[data-rv-source='sources.registrationNumbers']"));
		searchElement.sendKeys(Owner);
		Thread.sleep(600);
		searchElement.sendKeys(Keys.ENTER);
		Thread.sleep(1500);
		searchElement.sendKeys(Keys.ENTER);
		Thread.sleep(2500);
		// balanceOld =
		// Integer.parseInt(driver.findElement(By.cssSelector("[data-rv-value='currentCar.ownerDriver.balance']")).getAttribute("value"));
		// ratingOld = Float.parseFloat(driver.findElement(By.cssSelector("[data-rv-value='currentCar.rating']")).getAttribute("value"));
		// kolvoReviewOld = Integer.parseInt(driver.findElement(By.cssSelector("[class='badge pull-left js-grid-review-count']")).getText());
		//
		// System.out.println("" + balanceOld);
		// System.out.println("" + ratingOld);
		// System.out.println("" + kolvoReviewOld);

		Thread.sleep(500);
		WebElement submitElement = driver.findElement(By.cssSelector("[data-action='addReview']"));
		submitElement.click();
		Thread.sleep(1000);

		ArrayList<Integer> opt = new ArrayList<Integer>();
		int kolOptions = 0;

		kolOptions = getNumber(driver, kolOptions, opt);

		int kolRating = 1 + (int) (Math.random() * (kolOptions - 1));
		int kolRatingTemp = kolRating;

		while (kolRating >= 0) {
			System.out.print("Size - " + opt.size() + " " + "Kol - " + kolOptions + " ");

			int Rating = (int) (Math.random() * 10);

			ratingA.add(Rating);
			ratingCalculated = ratingCalculated + Rating;

			driver.findElement(By.cssSelector("[data-rv-value='newReview.parameters." + opt.get(kolRating) + ".rating']")).sendKeys("" + Rating);
			System.out.print("kolRating - " + kolRating + " ");
			System.out.print("Rating - " + Rating + " ");
			System.out.println("Opt.size - " + opt.get(kolRating) + " ");
			opt.remove(kolRating);
			kolOptions--;
			kolRating--;
		}

		ratingCalculated = ratingCalculated / kolRatingTemp;

		System.out.println(" ");
		WebElement addInsElement = driver.findElement(By.cssSelector("[data-action='AddReviewReal']"));
		addInsElement.click();
		sorting(driver);
		dataReading(driver);
		curData(driver);
	}

	public static void sorting(WebDriver driver) throws InterruptedException {
		driver.findElement(By.cssSelector("[id$='createdAt']")).click();
		driver.findElement(By.cssSelector("[id$='createdAt']")).click();
		Thread.sleep(5000);
	}

	public static void dataReading(WebDriver driver) throws InterruptedException {
		dataLastReviewOld = driver
				.findElement(
						By.xpath("/html/body/div[@class='application js-sidebar-min ']/div[@class='page-content-outer js-instance-change js-mass-sending js-app-root hide-weak js-loaded']/div[@class='page-content js-scrollable']/div[@class='content-outer']/div[@class='content-bottom tab-content']/div[@id='tab-reviews']/div[@class='content-bottom']/div[@class='content-outer offset-top']/div[@class='content-bottom']/div[@class='grid-outer']/div[@class='grid-inner']/*/div[@class='slick-viewport']/div[@class='grid-canvas']/div[@class='slick-grid-content slick-row even'][1]/div[@class='slick-cell l0 r0']"))
				.getText();

		System.out.println(dataLastReviewOld);
		year = Integer.parseInt(dataLastReviewOld.substring(0, 4));
		month = Integer.parseInt(dataLastReviewOld.substring(5, 7));
		day = Integer.parseInt(dataLastReviewOld.substring(8, 10));
		Thread.sleep(2000);
		switch (month) {
		case 1:
			dayZ = 0;
			break;
		case 2:
			dayZ = 31;
			break;
		case 3:
			dayZ = 59;
			break;
		case 4:
			dayZ = 90;
			break;
		case 5:
			dayZ = 120;
			break;
		case 6:
			dayZ = 151;
			break;
		case 7:
			dayZ = 181;
			break;
		case 8:
			dayZ = 212;
			break;
		case 9:
			dayZ = 243;
			break;
		case 10:
			dayZ = 273;
			break;
		case 11:
			dayZ = 304;
			break;
		case 12:
			dayZ = 334;
			break;
		}

		switch (year % 4) {
		case 1:
			break;
		case 2:
			break;
		case 3:
			break;
		case 0:
			dayZ = dayZ + 1;
			break;
		}

		dayZ = dayZ + day;
		System.out.println("" + dayZ);
	}

	public static void curData(WebDriver driver) throws InterruptedException {
		Calendar calendar = Calendar.getInstance();

		curDay = calendar.get(Calendar.DAY_OF_YEAR);
		System.out.println(curDay);
	}

	public static void dateParse(WebDriver driver) throws InterruptedException {

	}
}